#!/bin/bash

# shellpack bound to commit 8d22ab8 ("S benchmark suite 3.4")
###SHELLPACK preamble S-startup-install 8d22ab8
GIT_LOCATION="https://github.com/Algodev-github/S.git"
MIRROR_LOCATION="$WEBROOT/S"

install-depends gawk gnuplot coreutils fio time sysstat gcc-c++
install-depends xterm libreoffice-writer gnome-terminal sysstat

# Done separately as openSUSE requires packman repo for MPlayer
install-depends MPlayer

###SHELLPACK parseargBegin
###SHELLPACK parseargEnd

###SHELLPACK git_fetch S-startup-${VERSION}.tar.gz S-startup-${VERSION}-installed

# Specify standard required to build some tools
echo Patching S-startup for C++ standards
sed -i -e 's/g++ -pthread/g++ -std=c++11 -pthread/' comm_startup_lat/comm_startup_lat.sh

# Put kernel devel tree in place that S expects
if [ "$S_STARTUP_REQUIRES_KERNELTREE" = "yes" ]; then
	install-depends git-core
	pushd $SHELLPACK_DATA || exit $SHELLPACK_ERROR
	echo Attempting fetch from $MIRROR_LOCATION/../gitcheckout/linux-2.6.tar.gz
	wget -q $MIRROR_LOCATION/../gitcheckout/linux-2.6.tar.gz
	if [ $? -eq 0 ]; then
		echo Using mirrored tarball
		tar -xf linux-2.6.tar.gz || die "Failed to extract mirrored linux-2.6 tarball"
		DST_DIR=`tar tf linux-2.6.tar.gz | head -n 1 | awk -F / '{print $1}'`
		mv $DST_DIR linux-2.6
	else
		echo Cloning git repository
		git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6 || die "Failed to clone Linus git tree"
	fi
	mv linux-2.6 linux.git-for_kern_dev_benchmarks
	cd linux.git-for_kern_dev_benchmarks

	echo Updating git repository
	git remote update

	echo Checking out v4.15 for a consistent kernel build environment
	git checkout v4.15 || die "Failed to checkout v4.15"
fi
sync

echo S-startup installed successfully
